﻿// Decompiled with JetBrains decompiler
// Type: Netick.SimpleAllocator
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;
using System.Collections.Generic;

#nullable disable
namespace Netick;

internal class SimpleAllocator : ISafeAllocator, IAllocator
{
  private List<IntPtr> _blocks = new List<IntPtr>(32 /*0x20*/);

  unsafe ~SimpleAllocator()
  {
    for (int index = 0; index < this._blocks.Count; ++index)
      MemoryAllocation.Free(this._blocks[index].ToPointer());
    this._blocks.Clear();
  }

  public unsafe void* Allocate(ulong sizeBytes, out int poolIndex, out long offset, bool clearMem)
  {
    return this.SafeMalloc(sizeBytes, out poolIndex, out offset, clearMem);
  }

  public unsafe void Deallocate(void* ptr)
  {
    this._blocks.Remove(new IntPtr(ptr));
    MemoryAllocation.Free(ptr);
  }

  public unsafe void* SafeMalloc(
    ulong sizeBytes,
    out int poolIndex,
    out long offset,
    bool clearMem)
  {
    void* ptr = MemoryAllocation.Malloc((long) sizeBytes);
    if (clearMem)
      MemoryAllocation.Clear(ptr, (long) sizeBytes);
    poolIndex = 0;
    offset = 0L;
    this._blocks.Add(new IntPtr(ptr));
    return ptr;
  }
}
